package com.koudai.finance.utils;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

@Slf4j
public class StockUtil {

    public static final String token = "c896a019dfb64812bbe77bbc4ebed42c";

    /**
     * @Description: 获取所有股票
     * @author: ls
     * exchange交易所代码-XSHG（上交所）、XSHE（深交所）; code-股票代码; isActive-是否活跃。0：不活跃，1：活跃，2：所有。判断标准：近1个月是否有交易。默认：2
     * fmt-输出格式。支持json和csv两种标准输出格式，默认：json
     * @Date: 2020/7/10 16:00
     */
    public static JSONObject getStockList(String exchange, String code, String isActive, String fmt) {
        JSONObject jsonObject;
        String url = "https://tsanghi.com/api/fin/stock/" + exchange + "/list?token=" + token;
        if (StringUtils.isNotBlank(code)) {
            url = url + "&ticker=" + code;
        }
        if (StringUtils.isNotBlank(isActive)) {
            url = url + "&is_active=" + isActive;
        }
        if (StringUtils.isNotBlank(fmt)) {
            url = url + "&fmt=" + fmt;
        }
        jsonObject = HttpClientUtils.httpGet(url, null);
        if (!StringUtils.equals(jsonObject.getString("code"), "200")) {
            log.error("调用股票清单接口失败: {}", jsonObject);
            return jsonObject;
        }
        return jsonObject;
    }

    /**
     * @Description: 获取周线历史数据
     * @author: ls
     * exchange交易所代码-XSHG（上交所）、XSHE（深交所）; code-股票代码; fmt-输出格式。支持json和csv两种标准输出格式，默认：json
     * @Date: 2020/7/10 16:00
     */
    public static JSONObject getWeekData(String exchange, String code, String fmt) {
        JSONObject jsonObject;
        String url = "https://tsanghi.com/api/fin/stock/" + exchange + "/weekly?token=" + token + "&ticker=" + code + "&limit=60&order=2";
        if (StringUtils.isNotBlank(fmt)) {
            url = url + "&fmt=" + fmt;
        }
        jsonObject = HttpClientUtils.httpGet(url, null);
        if (!StringUtils.equals(jsonObject.getString("code"), "200")) {
            log.error("调用周线历史数据接口失败: {}", jsonObject);
            return jsonObject;
        }
        return jsonObject;
    }
}
